package kr.hyosang.dkbs.kiosk;

import java.net.URL;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.text.Font;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class AppMain extends Application {

	@Override
	public void start(Stage stage) throws Exception {
		//세로 스크린 검색
		Screen targetScreen = null;
		ObservableList<Screen> list = Screen.getScreens();
		for(Screen s : list) {
			Rectangle2D rect = s.getBounds();

			Log.log("SCREEN : " + rect.getWidth() + " * " + rect.getHeight());
			Log.log("X : " + rect.getMinX() + " ~ " + rect.getMaxX() + " / " + "Y : " + rect.getMinY() + " ~ " + rect.getMaxY());

			if(rect.getWidth() < rect.getHeight()) {
				//세로 스크린
				targetScreen = s;
				break;
			}
		}
		
		
		ClassLoader cl = AppMain.class.getClassLoader();
		String mainLayout = "fxml/main_768_1360.fxml";
		URL urlMain = null;
		Rectangle2D screenBound = null;
		
		if(targetScreen == null) {
			screenBound = Screen.getPrimary().getBounds();
		}else {
			screenBound = targetScreen.getBounds();
		}
		
		String screen = String.format("fxml/main_%d_%d.fxml",
				(int)screenBound.getWidth(),
				(int)screenBound.getHeight());
		urlMain = cl.getResource(screen);
		
		if(urlMain == null) {
			urlMain = cl.getResource(mainLayout);
		}
		
		Parent root;
		try {
			FXMLLoader loader = new FXMLLoader(urlMain);
			root = (Parent) loader.load();
		}catch(Exception e) {
			e.printStackTrace();
			return;
		}
		
		Scene scene = new Scene(root);
		stage.setScene(scene);
		
		if(targetScreen == null) {
			//가로화면에 세로로 고정시킴
			stage.setWidth(screenBound.getHeight());
			stage.setHeight(screenBound.getWidth());
			stage.setX(0);
			stage.setY(0);
		}else {
			//세로화면에 위치
			stage.setX(targetScreen.getBounds().getMinX());
			stage.setY(targetScreen.getBounds().getMinY());
			
        if(targetScreen.getBounds().getWidth() == 768 && targetScreen.getBounds().getHeight() == 1366) {
            stage.setFullScreen(true);
	      }else {
	      stage.setWidth(768 + 16);
				stage.setHeight(1360 + 38);
	      }
		}
		
		//gets root
		stage.setOnShown(new EventHandler<WindowEvent>() {
			@Override
			public void handle(WindowEvent event) {
				Stage stage = (Stage)event.getSource();
				stage.getScene().setOnKeyReleased(new EventHandler<KeyEvent>() {
					@Override
					public void handle(KeyEvent evt) {
						if(evt.getCode() == KeyCode.F12) {
							Scene scene = (Scene)evt.getSource();
							((Stage)(scene.getWindow())).setFullScreen(true);
						}
					}
				});
			}
		});		
		
		stage.show();
		
		Log.log(scene.getWidth() + " / "  + scene.getHeight());
		
		stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
			@Override
			public void handle(WindowEvent event) {
				System.exit(0);
			}
			
		});

	}
	
	@Override
	public void init() throws Exception {
		super.init();

		Font f = null;
		try {
			String u = AppMain.class.getClassLoader().getResource("font/HANDotum.ttf").toExternalForm();
			System.out.println("Font URL : " + u);
			f = Font.loadFont(u, 10);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		if(f == null) System.out.println("Font load failed");
		else System.out.println("Font loaded : " + f.getFamily());
	}
	
	public static void main(String [] args) {
		Env.load();
		
		launch(args);
	}

}
